package com.drakulo.hi.states;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import com.dk.hi.core.Settings;
import com.drakulo.hi.gfx.GameViewPort;
import com.drakulo.hi.gfx.MenuButton;
import com.drakulo.hi.gfx.panel.FamePanel;
import com.drakulo.hi.gfx.panel.InventoryPanel;
import com.drakulo.hi.gfx.panel.MapPanel;
import com.drakulo.hi.gfx.panel.Panel;
import com.drakulo.hi.gfx.panel.StatePanel;

public class MenuState extends BasicGameState {
	public static final int MENU_WIDTH = 152;
	public static final int BUTTON_PADDING = 30;

	private MenuButton inventoryBtn;
	private MenuButton fameBtn;
	private MenuButton stateBtn;
	private MenuButton mapBtn;

	List<MenuButton> buttons;

	private Panel currentPanel;

	@Override
	public int getID() {
		return 0;
	}

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
		// Initialisation du panneau par défaut : la carte
		this.currentPanel = new MapPanel();

		int paddingX = 5;
		int paddingY = BUTTON_PADDING;
		int y = 0;

		this.mapBtn = new MenuButton(this, MapPanel.class, "Carte", paddingX, y);
		this.mapBtn.select();

		y += paddingY;
		this.inventoryBtn = new MenuButton(this, InventoryPanel.class, "Inventaire", paddingX, y);

		y += paddingY;
		this.fameBtn = new MenuButton(this, FamePanel.class, "Réputation", paddingX, y);

		y += paddingY;
		this.stateBtn = new MenuButton(this, StatePanel.class, "Statut", paddingX, y);

		this.buttons = new ArrayList<MenuButton>();
		this.buttons.add(this.mapBtn);
		this.buttons.add(this.inventoryBtn);
		this.buttons.add(this.fameBtn);
		this.buttons.add(this.stateBtn);
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
		// Rendu du monde en fond
		GameViewPort.render(g);

		// Création d'un filtre Alpha
		g.setColor(new Color(0F, 0F, 0F, 0.5F));
		g.fillRect(0, 0, Settings.WINDOW_WIDTH, Settings.WINDOW_HEIGHT);

		// Rendu du menu
		renderMenuGUI(g);

		// Rendu du panneau affiché
		this.currentPanel.render(g);
	}

	private void renderMenuGUI(Graphics g) throws SlickException {
		// Rendu du fond des boutons
		g.fillRect(0, 0, MENU_WIDTH, Settings.WINDOW_HEIGHT);

		for (MenuButton b : this.buttons) {
			b.render(g);
		}
	}

	public void setPanel(Panel panel) {
		this.currentPanel = panel;
	}

	public void unSelectAllButtons() {
		for (MenuButton b : this.buttons) {
			b.unSelect();
		}
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int arg2) throws SlickException {
		Input input = container.getInput();
		if (input.isKeyPressed(Input.KEY_ESCAPE)) {
			game.enterState(0);
		}

		for (MenuButton b : this.buttons) {
			b.update(input);
		}
		this.currentPanel.update(input);
	}
}
